<?php

namespace app\models;


use app\librarys\Tools;
use yii\mongodb\ActiveRecord;

class ApiExceptionLog extends ActiveRecord
{
    /**
     * @param $actionId
     * @param $getParams
     * @param $postParams
     * @param $account
     * @param $timestamp
     * @param $sign
     * @param $remoteAddr
     * @param $httpUserAgent
     * @param $createDatetime
     * @param \Exception $ex
     * @return null|string
     */
    public static function add($actionId, $getParams, $postParams, $account, $timestamp, $sign, $remoteAddr, $createDatetime, $ex)
    {
        $log = new ApiExceptionLog();
        $log->_id = Tools::uuid();
        $log->actionId = $actionId;
        $log->getParams = $getParams;
        $log->postParams = $postParams;
        $log->account = $account;
        $log->timestamp = $timestamp;
        $log->sign = $sign;
        $log->remoteAddr = $remoteAddr;
        $log->createDatetime = $createDatetime;
        $log->exceptionCode = $ex->getCode();
        $log->exceptionMessage = $ex->getMessage();
        $log->exceptionTraceMessage = Tools::getExceptionFullTraceAsString($ex);// $ex->getTraceAsString();
        $result = $log->save();
        if ($result == true) {
            return $log->_id;
        } else {
            return null;
        }
    }


    public static function getById($id)
    {
        $log = self::find()->where(['_id' => $id])->one();
        return $log;
    }

    public static function getCount($actionId, $account, $startTime, $endTime)
    {
        $whereParams = [];
        if (!empty($actionId)) {
            $whereParams['actionId'] = $actionId;
        }
        if (!empty($account)) {
            $whereParams['account'] = $account;
        }

        $items = self::find()->where($whereParams);


        if (!empty($startTime)) {
            $stime = strtotime($startTime);
            if (!empty($whereParams)) {
                $items->andWhere(['>=', 'createDatetime', $stime]);
            } else {
                $items->where(['>=', 'createDatetime', $stime]);
            }
        }
        if (!empty($endTime)) {
            $etime = strtotime($endTime);
            if (!empty($whereParams) || !empty($startTime)) {
                $items->andWhere(['<', 'createDatetime', $etime]);
            } else {
                $items->where(['<', 'createDatetime', $etime]);
            }
        }
        return $items->count();
    }


    public static function getList($page, $pageSize, $actionId, $account, $startTime, $endTime)
    {
        $whereParams = [];
        if (!empty($actionId)) {
            $whereParams['actionId'] = $actionId;
        }
        if (!empty($account)) {
            $whereParams['account'] = $account;
        }

        $items = self::find()->where($whereParams);

        if (!empty($startTime)) {
            $stime = strtotime($startTime);
            if (!empty($whereParams)) {
                $items->andWhere(['>=', 'createDatetime', $stime]);
            } else {
                $items->where(['>=', 'createDatetime', $stime]);
            }

        }
        if (!empty($endTime)) {
            $etime = strtotime($endTime);
            if (!empty($whereParams) || !empty($startTime)) {
                $items->andWhere(['<', 'createDatetime', $etime]);
            } else {
                $items->where(['<', 'createDatetime', $etime]);
            }
        }

        return $items->offset($page * $pageSize)
            ->limit($pageSize)
            ->orderBy('createDatetime desc')
            ->asArray()
            ->all();
    }


    public function attributes()
    {
        return [
            '_id',  // pk api请求日志
            'actionId',  // 请求的 action id
            'getParams',  // 请求的get参数数组
            'postParams',  // 请求的post参数数组
            'account',
            'timestamp',  // get请求中的时间戳
            'sign',  // get请求中的签名
            'remoteAddr',  // 访问的来源地址ip
            'createDatetime',  // 请求时间
            'exceptionCode',
            'exceptionMessage',
            'exceptionTraceMessage'
        ];
    }
}